package com.yonyou.pmclouds.workstartreport.process;

import com.yonyou.pmclouds.basecom.bp.processor.SingleProcessor;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.util.BeanHelper;
import com.yonyou.pmclouds.basecom.util.CommonFieldConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import yonyou.bpm.rest.request.AssignInfoItem;
import yonyou.bpm.rest.request.Participant;

/**
 * 判断当前用户是否有是审批人
 * @param <T>
 */
@Component
@Slf4j
public class WorkFolwAssignInfoItemProcess<T extends SuperVO> implements SingleProcessor<T> {
    @Override
    public void process(T vo, T orig) throws BusinessException {
        BeanHelper.setProperty(vo, CommonFieldConst.CAN_APPROVE, false);
        if(isSame(vo)){
            BeanHelper.setProperty(vo, CommonFieldConst.CAN_APPROVE, true);
        }
    }

    public boolean isSame(T vo){
        boolean property = (boolean)BeanHelper.getProperty(vo, CommonFieldConst.CAN_APPROVE);

        String creator = (String)BeanHelper.getProperty(vo, CommonFieldConst.CREATOR);

        if(vo.assignInfo == null){
            return false;
        }

        AssignInfoItem[] assignInfoItems = vo.assignInfo.getAssignInfo().getAssignInfoItems();
        for(AssignInfoItem assignInfoItem : assignInfoItems){
            Participant[] participants = assignInfoItem.getParticipants();
            for(Participant participant : participants){
                if(creator.equals(participant.getId())){
                    return true;
                }
            }
        }
        return false;
    }
}
